package com.milorad.ads.pages.admin;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.milorad.ads.data.dao.interfaces.CategoryDao;
import com.milorad.ads.data.domain.Category;
import com.milorad.ads.pages.BasePage;

public class CategoriesAdministrationPage extends BasePage {
	
	@SpringBean
	private CategoryDao categoryDao;
	
	public CategoriesAdministrationPage() {				
		
		// new category link
		add(createNewCategoryLink());
		
		// list with all categories, and change option 
		add(createCategoriesList());
		
		add (new BookmarkablePageLink<Void>("adminPanelLink", MainAdminPage.class));
	}

	private Link<String> createNewCategoryLink() {
		
		return new Link<String>("newCategoryLink") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new CreateEditCategoryPage(new Category()));				
			}			
		};
	}

	private ListView<Category> createCategoriesList() {
		
		return new ListView<Category>("categoriesList", categoryDao.findAll()) {

			private static final long serialVersionUID = -4083520522187567842L;

			@Override
			protected void populateItem(ListItem<Category> item) {
				Category category = item.getModelObject();
				item.add(new Label("id", category.getId().toString()));
				item.add(new Label("name", category.getName()));
				item.add(new Label("description", category.getDescription()));	

				item.add(new Link<Category>("change", item.getModel()) {
					
					private static final long serialVersionUID = 2221207599260261693L;
					
					@Override
					public void onClick() {
						Category category = getModelObject();
						setResponsePage(new CreateEditCategoryPage(category));
						
					}					
				});
			}						
		};
	}
}